package org.massdeception.content.servlets.authors;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.massdeception.content.entities.MOTD;
import org.massdeception.content.entities.PMF;
import org.massdeception.content.entities.Slide;
import org.massdeception.content.util.SlideUtils;


import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;

public class AuthorSelectSlidesServlet extends AuthorServlet 
{
    private static final Logger log = Logger.getLogger(AuthorSelectSlidesServlet.class.getName());
    
    public void process(HttpServletRequest request, HttpServletResponse response, User user)
        throws IOException, ServletException
    {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try
        {            
            long today = SlideUtils.getNormalizedDate(new Date(), true).longValue();
            
            //log.log(Level.WARNING, "today: "+today);
            
            Query query = pm.newQuery(Slide.class);
            query.setFilter("publishDate <= publishDateMaxParam && publishDate >= publishDateMinParam");
            query.setRange(0, 100);
            query.setOrdering("publishDate desc");
            query.declareParameters("Long publishDateMaxParam, Long publishDateMinParam");
            List<Slide> results = (List<Slide>) query.execute(new Long(today+50), new Long(today-50));
            
            request.setAttribute("slides", results);
            
            // fill the current MOTD
            MOTD motd = null;
            Key key = KeyFactory.createKey(MOTD.class.getSimpleName(), "motd");
            try
            {
                motd = (MOTD) pm.getObjectById(MOTD.class, key);
            }
            catch (Throwable t)
            {
                log.log(Level.WARNING, "No MOTD in data store, creating new one with no values");
                motd = new MOTD();
                motd.setMessage("");
                motd.setUpdated(new Date());
                motd.setUser(user);
                motd.setkey(key);
                pm.makePersistent(motd);
                SlideUtils.setMOTD(motd);
            }
            request.setAttribute("motd", motd);
            
            RequestDispatcher dispatcher = request.getRequestDispatcher("/authorSelectSlide.jsp");
            dispatcher.forward(request, response);
            
            return;
        }
        catch (Throwable t)
        {
            log.log(Level.WARNING, "Could not query for most current slides", t);
            response.sendError(500, "Request could not be processed.");
            return;
        }
        finally
        {
            pm.close();
        }
    }
}